import { _decorator, Animation, AudioClip, Camera, Component, director, Node, resources} from 'cc';
import { CardGenerator } from './CardGenerate';
import { GLOBALPOP } from './GlobalPop';
import { SoundManager } from './SoundManager';
import { WebSocketManager } from './WebSocketManager';
import { GLOBALVar } from './GlobalVar';
const { ccclass, property } = _decorator;

@ccclass('GameManager')
export class GameManager extends Component {
    private static _instance: GameManager = null;

    @property({type: Node, tooltip: "卡牌容器"})
    private CardContainer: Node | null = null;
    @property({type: Camera, tooltip: "摄像机"})
    private Camera: Camera | null = null;
    @property({type: Node, tooltip: "失败面板"})
    private FailedPanel: Node | null = null;
    @property({type: Node, tooltip: "设置面板"})
    private SettingPanel: Node | null = null;
    
     async onLoad() {
        // 提前触发 SoundManager 初始化
        SoundManager.getInstance();
        //加载BGM
        resources.load('Audios/dgqgdyz_vocaloid', AudioClip, (err, clip) => {
            if (err) {
                console.error(err);
                return;
            }
            const soundManager = SoundManager.getInstance();
            soundManager.stopBGM();
            soundManager.playBGM(clip, 0.7);
        })
    }
    start() {
        GLOBALPOP.setFailedPanel(this.FailedPanel);
        GLOBALPOP.setCamera(this.Camera);
        GLOBALPOP.setCardContainer(this.CardContainer);
        CardGenerator.setCardContainer(this.CardContainer);
        CardGenerator.setCurrentCamera(this.Camera);
        CardGenerator.generateLevel();
    }
    update(deltaTime: number) {
        
    }
    
    onClickRestart() {
        CardGenerator.restart();
    }
    onClickNotPlay() {
        director.loadScene("MenuScene");
    }

    onClickSettingBtn() {
        this.SettingPanel.active = true;
        this.SettingPanel.getChildByName("BG_Setting").getComponent(Animation).play();
    }
}